<!-- 故障诊断详情表 -->

<template>
    <div class="wrapper">
        <!-- 报表Tabs组件-->
        <ReportTabs :curTab="curTab" :tabDatas="tabDatas" :isExport="isExport" @updateTab="updateTab"
            @handleExport="handleExport" />
        <!-- 动态组件-->
        <keep-alive>
            <FaultStaticTable v-if="curTab === '1'" @updateEventData="updateEventData" ref="staticTable" />
        </keep-alive>
        <keep-alive>
            <FaultExportTable v-if="curTab === '2'" :isAddTask="isAddTask" :queryParams="queryParams" />
        </keep-alive>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

import * as local from '@/utils/cache/localStorage';
import * as server from '@/api/modules/technicalDataAnalysis';

import ReportTabs from '@/components/reportTabs/index.vue';
import FaultStaticTable from './c/FaultStaticTable.vue';
import FaultExportTable from './c/FaultExportTable.vue';

const curTab = ref('1');
const isExport = ref(true);
const isAddTask = ref(false);
const staticTable = ref(null);
const queryParams = ref({});
const exportDatas = ref([]);

// tabs 数据
const tabDatas = ref([
    {
        id: '1',
        label: '故障诊断'
    },
    {
        id: '2',
        label: '离线导出'
    }
]);

const updateTab = (id) => {
    curTab.value = id;

    isExport.value = id === '1' ? true : false;
};

const updateEventData = (params) => {
    exportDatas.value = params.exportDatas;
    queryParams.value = params.queryParams;
};

// 导出
const handleExport = () => {
    if (exportDatas.value.length === 0) {
        ElMessage.warning('无查询数据导出!');
        return;
    }

    const callback = () => {
        const params = {
            equal: {
                ...queryParams.value.equal,
                createUser: local.getUser().id
            }
        };

        server.addFaultDetailExportTask(params)
            .then(res => {
                const { code } = res;
                if (code === '200') {
                    curTab.value = '2';
                    isAddTask.value = !isAddTask.value;
                    ElMessage.success('添加成功!');
                } else {
                    ElMessage.success('导出任务添加失败!');
                }
            })

    };

    staticTable.value.handleExport(callback);
};
</script>

<style lang="scss" scoped></style>